#ifndef CON_DETAIL_CHILD_PROCESS_RECORDER_H
#define CON_DETAIL_CHILD_PROCESS_RECORDER_H

#pragma once

#if !defined(UNICODE) || !defined(_UNICODE)
#define UNICODE
#define _UNICODE
#endif

#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0501 // XP is the minimum
#elif (_WIN32_WINNT < 0x0501)
#error "Multi console requires XP or above"
#endif

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif

#include <map>
#include <windows.h>

namespace boost
{
	template<class T> class weak_ptr;
	template<class T> class shared_ptr;
}

namespace Con
{
	class Console;
	typedef boost::weak_ptr<Console> ConsoleImplHandle;
	typedef boost::shared_ptr<Console> Handle;

	namespace detail
	{
		class ChildProcessRecorder
		{
		private:
			typedef std::map<DWORD, ConsoleImplHandle> Consoles;
			Consoles activeConsoles; // the map of consoles
			CRITICAL_SECTION cSection;
			PVOID exceptToken; // the token returned by AddVectoredExceptionHandler

		public:
			ChildProcessRecorder();
			~ChildProcessRecorder();

			// shutdown the console owning processes, 
			void Shutdown();

			// add the console to the map
			void Add(const Handle& console);

			// remove the console associated with 'id' and signal the owning process to exit
			void Remove(DWORD id);

			// get a handle to the console represented by 'id'
			bool Get(DWORD id, Handle& out);
		};
	}
}

#endif
